'use strict';
const db = uniCloud.database();

/**
 * 云函数：上传奖品
 * @param {Object} event - 请求参数
 * @param {string} event.name - 奖品名称
 * @param {number} event.points - 所需积分
 * @param {number} event.stock - 库存
 * @param {string} event.image - 图片URL
 * @returns {Object} - 上传结果
 */
exports.main = async (event, context) => {
  const { name, points, stock, image } = event;
  try {
    const itemCollection = db.collection('items');
    const itemId = 'item_' + Date.now();
    await itemCollection.add({
      _id: itemId,
      name,
      points,
      stock,
      image,
      created_at: Date.now()
    });
    return {
      code: 0,
      data: { item_id: itemId },
      msg: '上传奖品成功'
    };
  } catch (e) {
    console.error('上传奖品失败:', e);
    return { code: -1, msg: '上传奖品失败' };
  }
};